package com.evildog.workspace.life.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.NoteBookBO;
import com.evildog.workspace.life.application.bo.NoteBookListBO;
import com.evildog.workspace.life.application.bo.NoteBookPageBO;
import com.evildog.workspace.life.application.convert.NoteBookConvert;
import com.evildog.workspace.life.application.dto.NoteBookDTO;
import com.evildog.workspace.life.application.dto.NoteBookListDTO;
import com.evildog.workspace.life.application.dto.NoteBookPageDTO;
import com.evildog.workspace.life.application.entity.NoteBook;
import com.evildog.workspace.life.application.entity.NoteBookListDO;
import com.evildog.workspace.life.application.entity.NoteBookPageDO;
import com.evildog.workspace.life.application.mapper.NoteBookMapper;
import com.evildog.workspace.life.application.service.NoteBookService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class NoteBookServiceImpl extends ServiceImpl<NoteBookMapper, NoteBook> implements NoteBookService {

    @Override
    public PagedResult<NoteBookPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, NoteBookPageDTO pageDTO) {
        IPage<NoteBookPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return NoteBookConvert.INSTANCE.convert(page);
    }

    @Override
    public List<NoteBookListBO> getList(List<QueryConditionDTO> queryCondition, NoteBookListDTO dto) {
        List<NoteBookListDO> noteBookList = this.baseMapper.getList(queryCondition, dto);
        return NoteBookConvert.INSTANCE.convert(noteBookList);
    }

    @Override
    public NoteBookBO getDetailById(Long id) {
        NoteBook noteBook = this.getById(id);
        Assert.notNull(noteBook, "noteBook is null!");
        return NoteBookConvert.INSTANCE.convert(noteBook);
    }

    @Override
    public boolean save(NoteBookDTO dto){
        NoteBook newNoteBook = NoteBookConvert.INSTANCE.convert(dto);
        return save(newNoteBook);
    }

    @Override
    public boolean updateById(Long id, NoteBookDTO dto){
        NoteBook oldNoteBook = getById(id);
        Assert.notNull(oldNoteBook, "noteBook is null!");
        NoteBook newNoteBook = NoteBookConvert.INSTANCE.convert(dto);
        newNoteBook.setId(id);
        return updateById(newNoteBook);
    }
}
